<?php

class Usuarios_Model_PerfilMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable) {
		if (is_string ( $dbTable )) {
			$dbTable = new $dbTable ();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception ( 'Invalid table data gateway provided' );
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable ( 'Usuarios_Model_DbTable_Perfil' );
		}
		return $this->_dbTable;
	}
	
	private function _fillPerfil($row,$arr=FALSE){
		$registro = new Usuarios_Model_Perfil();
		$registro->setId($row->id)
		->setNome($row->nome)
		->setId_parent($row->nome);
		if($arr === false)
			$registro->setPparent($this->setPerfilParent($row->id_parent)->nome);
		else
			$registro->setPparent($row->id_parent);
		return $registro;
	}
	

	public function setPerfilParent($id) {
		if ($id > 0){
			$sql = $this->getDbTable()->select()->where('id = ?',$id);
			return $this->getDbTable()->fetchRow($sql);
		}else{
			$nome = new stdClass();
			$nome->nome = "";
			return $nome;
		}
	}
	public function listPerfils($opt=NULL)
	{
		$sql = $this->getDbTable()->select()->order(array("nome ASC", "id_parent ASC"));
		$resultSet = $this->getDbTable()->fetchAll($sql);
		$entries   = array();
		foreach ($resultSet as $row) {
			if ($opt)
				$entries[$row->id] = $row->nome;
			else
			$entries[] =  $this->_fillPerfil($row);
		}
		return $entries;
	}
	/*
	public function listPerfilsArreyxxx()
	{
		$sql = $this->getDbTable()->select()->order(array("nome ASC", "id_parent ASC"));
		$resultSet = $this->getDbTable()->fetchAll($sql);
		$entries   = array();
		foreach ($resultSet as $row) {
			$entries[] = $this->_fillPerfil($row);
		}
		return $entries;
	}
	*/
	
	
	public function fetchAll()
	{
		$sql = $this->getDbTable()->select();
		$resultSet = $this->getDbTable()->fetchAll($sql);
		$entries   = array();
		foreach ($resultSet as $row) {
			$entries[] = $this->_fillPerfil($row,true);
		}
		return $entries;
	}
	
	public function editPerfils(Usuarios_Model_Perfil $registro, $id = null) {
		$data = array (
				'nome' => $registro->getNome (),
				'id_parent' => $registro->getId_parent ()
		);
		$this->getDbTable ()->update ( $data, array (
				'id = ?' => $id
		) );
	}
	public function savePerfils(Usuarios_Model_Perfil $registro) {
		$data = array (
				'nome' => $registro->getNome (),
				'id_parent' => $registro->getId_parent ()
		);
		$this->getDbTable ()->insert ( $data );
	}
	public function deletar($id = NULL) {
		try {
			$result = $this->getDbTable ()->find ( $id );
			$this->getDbTable ()->delete ( array (
					'id = ?' => $id
			) );
		} catch ( Zend_Exception $e ) {
			echo $e;
		}
	}
	
	
	#######################################
	
	public function find($id, Usuarios_Model_Perfil $registro)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$registro = $this->_fillPerfil($row);
	}
	
	public function findpopule($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		return $data = array(
				"id" => $row->id,
				"nome" => $row->nome,
				"id_parent" => $row->id_parent
		);
	}
}

